/*
 * Copyright (c) 2018. J4dream Created.
 */

package com.j4dream.property.web;


import com.google.common.collect.ImmutableMap;
import com.j4dream.property.dao.GeneratorDao;
import com.j4dream.property.model.GeneratorDto;
import com.j4dream.property.model.Status;
import com.j4dream.property.util.ID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController()
@RequestMapping("/api")
public class GeneratorController {
    @Autowired
    private GeneratorDao generatorDao;

    @GetMapping("/generator")
    public Map<String, List<GeneratorDto>> getGeneratorList() {
        return ImmutableMap.of("list", generatorDao.getGeneratorList());
    }

    @PostMapping("/generator")
    public GeneratorDto createGenerator(@RequestBody @Valid GeneratorDto generatorDto) {
        generatorDto.setId(ID.get());
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        generatorDto.setOwner(authentication.getName());
        generatorDto.setStatus(Status.DRAFT);
        generatorDao.insertGenerator(generatorDto);
        return generatorDto;
    }

    @GetMapping("/generator/{id}")
    public GeneratorDto getGeneratorDetail() {
        return generatorDao.getGeneratorDetail();
    }
}
